﻿// Jinx AJAX Framework
// Copyright (C) 2008 Blake Hegerle
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

using System;
using System.Collections;
using System.Collections.Generic;

namespace Jinx.Models.Sequences
{
    public abstract class Sequence : ModelBase
    {
        public const string COUNT_NAME = "Count";

        protected Sequence(Type DerivedType, bool IsReadOnly, bool IsVariableLength)
            : base(DerivedType.Name, Members)
        {
        }

        private static IEnumerable<ModelMember> Members
        {
            get
            {
                return new[]
                       {
                           new ModelMember(COUNT_NAME, typeof (uint))
                       };
            }
        }

        public abstract IEnumerable Enumerable { get; }
    }

    public abstract class Sequence<T> : Sequence, IEnumerable<T>, IObjectSequence
    {
        private readonly bool isReadOnly;
        private readonly bool isVariableLength;

        protected Sequence(Type DerivedType, bool IsReadOnly, bool IsVariableLength)
            : base(DerivedType, IsReadOnly, IsVariableLength)
        {
            isReadOnly = IsReadOnly;
            isVariableLength = IsVariableLength;
        }

        public abstract uint Count { get; set; }

        public virtual bool IsReadOnly
        {
            get { return isReadOnly; }
        }

        public virtual bool IsVariableLength
        {
            get { return isVariableLength; }
        }

        public abstract T this[uint Index] { get; set; }

        public override IEnumerable Enumerable
        {
            get { return this; }
        }

        #region IEnumerable<T> Members

        public abstract IEnumerator<T> GetEnumerator();

        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }

        #endregion

        #region IObjectSequence Members

        public object GetObject(uint Index)
        {
            return this[Index];
        }

        #endregion

        public static string ModelMemberName(uint i)
        {
            return MemberNaming.ModelMemberName(i);
        }

        public static ModelMember MakeModelMember(uint i)
        {
            return MemberNaming.MakeModelMember<T>(i);
        }
    }
}